package com.books.controller.websocket;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import com.books.model.entity.common.People;
import com.books.model.entity.lang.Consts;
import com.books.model.entity.lang.ImUser;
import com.books.model.entity.lang.Results;
import com.books.model.entity.login_regis.UserLogin;
import com.books.service.im.impl.ChatServiceImpl;
import com.books.service.people.impl.PeopleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/chat")
public class ChatController extends BaseController {
    @Autowired(required = false)
    private ImUser imUser;
    @Autowired
    private UserLogin userLogin;
    @Autowired
    private PeopleServiceImpl peopleService;
    @Autowired
    private People people;
    @Autowired
    private ChatServiceImpl chatService;

    @GetMapping("/getMineAndGroupData")
    @ResponseBody
    public Results getMineAndGroupData(HttpServletRequest request){
        HttpSession session = request.getSession ();
        userLogin = (UserLogin) session.getAttribute ("user");
//        String phone = userLogin.getAccount ();

        //默认群
        Map<String, Object> group = new HashMap<> ();
        group.put("name", "云顶书屋聊天群");
        group.put("type", "group");
        group.put("avatar", "http://47.100.102.177:8080/app/images/common/素材-大书.png");
        group.put("id", Consts.IM_GROUP_ID);
        group.put("members", 0);
        System.out.println (people);

//        AccountProfile profile = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
            String phone = userLogin.getAccount ();
            people = peopleService.getPeopleByAccount (phone);
//        people=peopleService.getPeopleByAccount ("13088888888");
            imUser.setId (people.getPeopleId ());
            imUser.setUsername (people.getName ());
//        imUser.setAvatar (people.getHeadUrl ());
            imUser.setAvatar ("/boot/imgs/"+people.getHeadUrl ());
            imUser.setStatus (ImUser.ONLINE_STATUS);

        return Results.success(MapUtil.builder()
                .put("group", group)
                .put("mine", imUser)
                .map());

    }

    @GetMapping("/getGroupHistoryMsg")
    public Results getGroupHistoryMsg() {

        List<Object> messages = chatService.getGroupHistoryMsg(20);
        return Results.success(messages);
    }

}
